package com.weimob.mcloud.wechatwork.customer.third.core;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.core.base.MultiRequest;
import com.weimob.mcloud.wechatwork.core.base.MultiResponse;
import com.weimob.mcloud.wechatwork.core.base.SingleRequest;
import com.weimob.mcloud.wechatwork.core.base.SingleResponse;
import com.weimob.mcloud.wechatwork.core.constant.CustomerGroupConstant;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.Customer;
import com.weimob.mcloud.wechatwork.core.entity.relation.group.CustomerGroup;
import com.weimob.mcloud.wechatwork.core.entity.relation.group.CustomerGroupMember;
import com.weimob.mcloud.wechatwork.core.enums.group.GroupMemberTypeEnum;
import com.weimob.mcloud.wechatwork.core.service.group.CustomerGroupExportService;
import com.weimob.mcloud.wechatwork.core.service.group.request.*;
import com.weimob.mcloud.wechatwork.core.service.group.response.CustomerGroupExternalResponse;
import com.weimob.mcloud.wechatwork.core.service.group.response.CustomerGroupNonFriendRequest;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author chenliuzhu
 * @date 2022/12/15
 */
@Component
@Slf4j
public class CustomerGroupExportServiceProxy {
    @Reference
    CustomerGroupExportService customerGroupExportService;

    /**
     * 获取客户群详情
     * @param customerGroup customerGroup
     * @return CustomerGroup
     */
    public SingleResponse<CustomerGroup> fetchCustomerGroupDetail(CustomerGroup customerGroup, boolean needFullInfo,Map<String, Object> extInfo) {
        if(Objects.isNull(customerGroup)){
            log.error("CustomerGroupExportServiceProxy.fetchCustomerGroupDetail 入参customerGroup为空");
            return null;
        }
        SingleRequest<CustomerGroup> singleRequest = new SingleRequest<>();
        singleRequest.setNeedFullInfo(needFullInfo);
        singleRequest.setData(customerGroup);
        singleRequest.setExtInfo(extInfo);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupDetail(singleRequest));
    }

    /**
     * 获取客户群成员列表
     * @param customerGroupMember
     * @param needFullInfo
     * @param needPage
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroupMember> fetchCustomerGroupMemberList(CustomerGroupMember customerGroupMember, Map<String, Object> extInfo, Boolean needFullInfo, Boolean needPage, Integer pageNum, Integer pageSize) {
        SingleRequest<CustomerGroupMember> singleRequest = new SingleRequest<>();
        singleRequest.setNeedPage(needPage);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        singleRequest.setNeedFullInfo(needFullInfo);
        singleRequest.setData(customerGroupMember);
        singleRequest.setExtInfo(extInfo);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupMemberList(singleRequest));
    }


    /**
     * 根据客户id查询客户群最早入群时间
     * @param customer
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroupMember> fetchGroupMemberInfoByExternalUser(Customer customer, PrivateSphere privateSphere, Integer pageNum, Integer pageSize){
        SingleRequest<CustomerGroupMember> singleRequest = new SingleRequest<>();
        singleRequest.setNeedPage(true);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        singleRequest.setNeedFullInfo(false);

        CustomerGroupMember customerGroupMember = new CustomerGroupMember();
        customerGroupMember.setCustomer(customer);
        customerGroupMember.setPrivateSphere(privateSphere);
        customerGroupMember.setGroupMemberType(GroupMemberTypeEnum.EXTERNAL_USER);
        singleRequest.setData(customerGroupMember);
        Map<String, Object> extInfo = new HashMap<>(16);
        extInfo.put(CustomerGroupConstant.ORDER_BY,"join_time");
        extInfo.put(CustomerGroupConstant.DIRECTION,"asc");
        singleRequest.setExtInfo(extInfo);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupMemberList(singleRequest));
    }

    /**
     * 根据groupId查询客户群信息
     * @param customerGroup
     * @param extInfo
     * @param groupIdList
     * @param needFullInfo
     * @param needPage
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroup> fetchCustomerGroupList(CustomerGroup customerGroup, Map<String, Object> extInfo, List<String> groupIdList, List<String> nameList, Boolean needFullInfo, Boolean needPage, Integer pageNum, Integer pageSize, boolean fetchGuider) {
        SingleGroupAttachRequest<CustomerGroup> singleRequest = new SingleGroupAttachRequest<>();
        singleRequest.setGroupIds(groupIdList);
        singleRequest.setNameList(nameList);
        singleRequest.setData(customerGroup);
        singleRequest.setExtInfo(extInfo);
        singleRequest.setNeedFullInfo(needFullInfo);
        singleRequest.setNeedPage(needPage);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        singleRequest.setFetchGuider(fetchGuider);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupList(singleRequest));
    }

    /**
     * 查询客户群列表(ES)
     * @param customerGroup
     * @param needPage
     * @param needFullInfo
     * @param pageNum
     * @param pageSize
     * @param extInfo
     * @param ownerIdList
     * @param groupIds
     * @param begin
     * @param end
     * @return
     */
    public MultiResponse<CustomerGroup> fetchCustomerGroupListES(CustomerGroup customerGroup, boolean needPage, boolean needFullInfo, int pageNum, int pageSize, Map<String, Object> extInfo, List<String> ownerIdList, List<String> groupIds, Date begin, Date end, boolean fetchGuider) {
        SingleGroupAttachRequest<CustomerGroup> singleGroupAttachRequest = new SingleGroupAttachRequest<>();
        singleGroupAttachRequest.setData(customerGroup);
        singleGroupAttachRequest.setNeedFullInfo(needFullInfo);
        singleGroupAttachRequest.setNeedPage(needPage);
        singleGroupAttachRequest.setPageNum(pageNum);
        singleGroupAttachRequest.setPageSize(pageSize);
        singleGroupAttachRequest.setOwnerIdList(ownerIdList);
        singleGroupAttachRequest.setGroupIds(groupIds);
        singleGroupAttachRequest.setExtInfo(extInfo);
        singleGroupAttachRequest.setDateRangeBegin(begin);
        singleGroupAttachRequest.setDateRangeEnd(end);
        singleGroupAttachRequest.setFetchGuider(fetchGuider);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupListES(singleGroupAttachRequest));
    }

    /**
     * 批量保存客户群managedVid
     *
     * @param groupManagedVidDTO
     */
    public void batchSaveGroupManagedVid(GroupManagedVidDTO groupManagedVidDTO) {
        SoaUtil.unpack(customerGroupExportService.batchSaveGroupManagedVid(groupManagedVidDTO));
    }



    /**
     * 获取群信息携带 "贫血" 群成员
     * @param privateSphere 私域
     * @param chatId 群ID
     * @return CustomerGroup
     */
    public CustomerGroup getGroupWithAnemicMember(PrivateSphere privateSphere, String chatId) {
        if (Objects.isNull(privateSphere) || Objects.isNull(chatId)) {
            log.error("CustomerGroupExportServiceProxy.getGroupAndMember 入参为空");
            return null;
        }

        // 群
        SingleRequest<CustomerGroup> groupSingleRequest = new SingleRequest<>();
        CustomerGroup customerGroup = new CustomerGroup();
        customerGroup.setPrivateSphere(privateSphere);
        customerGroup.setGroupId(chatId);
        groupSingleRequest.setData(customerGroup);
        SingleResponse<CustomerGroup> singleResponse = SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupDetail(groupSingleRequest));
        if (Objects.isNull(singleResponse) || Objects.isNull(singleResponse.getData())) {
            return null;
        }
        CustomerGroup resultCustomerGroup = singleResponse.getData();
        // 群成员
        SingleRequest<CustomerGroupMember> memberSingleRequest = new SingleRequest<>();
        CustomerGroupMember customerGroupMemberReq = new CustomerGroupMember();
        customerGroupMemberReq.setPrivateSphere(privateSphere);
        customerGroupMemberReq.setGroupId(chatId);
        memberSingleRequest.setNeedPage(false);
        memberSingleRequest.setNeedFullInfo(false);
        memberSingleRequest.setData(customerGroupMemberReq);
        MultiResponse<CustomerGroupMember> memberMultiResponse = SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupMemberList(memberSingleRequest));
        List<CustomerGroupMember> resultMemberList = memberMultiResponse.getDataList();
        resultCustomerGroup.setGroupMembers(resultMemberList);

        return resultCustomerGroup;
    }

    /**
     * 查询客户所属群
     * @param privateSphere
     * @param customerGroupMember
     * @param extInfo
     * @param needFullInfo
     * @param needPage
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroup> fetchCustomerGroupListByMember(PrivateSphere privateSphere, CustomerGroupMember customerGroupMember, Map<String, Object> extInfo, Boolean needFullInfo, Boolean needPage, Integer pageNum, Integer pageSize) {
        SingleRequest<CustomerGroupMember> singleRequest = new SingleRequest<>();
        singleRequest.setPrivateSphere(privateSphere);
        customerGroupMember.setPrivateSphere(privateSphere);
        singleRequest.setData(customerGroupMember);
        singleRequest.setExtInfo(extInfo);
        singleRequest.setNeedFullInfo(needFullInfo);
        singleRequest.setNeedPage(needPage);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        SoaResponse<MultiResponse<CustomerGroup>, ?> groupSoaResponse = customerGroupExportService.fetchCustomerGroupListByMember(singleRequest);
        return groupSoaResponse.getResponseVo();
    }

    /**
     * 更新群成员表好友标识
     */
    public void updateMemberFriendStatus(CustomerGroupMember customerGroupMember,Map<String, Object> extInfo) {
        SingleRequest<CustomerGroupMember> request = new SingleRequest<>();
        request.setData(customerGroupMember);
        if(Objects.nonNull(extInfo)){
            request.setExtInfo(extInfo);
        }
        customerGroupExportService.updateMemberFriendStatus(request);
    }

    /**
     * 客户群DB数据 写到 ES
     * @param customerGroup
     */
    public void saveCustomerGroup2ES(CustomerGroup customerGroup){
        SingleRequest<CustomerGroup> request = new SingleRequest<>();
        request.setData(customerGroup);
        customerGroupExportService.saveCustomerGroup2ES(request);
    }

    /**
     * 修改客户群授权状态
     * @param customerGroupList
     */
    public void updateGroupAuthorizationStatus(List<CustomerGroup> customerGroupList){
        MultiRequest<CustomerGroup> multiRequest = new MultiRequest<>();
        multiRequest.setData(customerGroupList);
        customerGroupExportService.updateGroupAuthorizationStatus(multiRequest);
    }


    /**
     * 查询客户群中非企业好友数
     * @param customerGroupNonFriendRequest
     * @return
     */
    public MultiResponse<CustomerGroupNonFriendResponse> batchFetchNonFriendNum(CustomerGroupNonFriendRequest customerGroupNonFriendRequest) {
        SingleRequest<CustomerGroupNonFriendRequest> singleRequest = new SingleRequest<>();
        singleRequest.setData(customerGroupNonFriendRequest);
        return SoaUtil.unpack(customerGroupExportService.batchFetchNonFriendNum(singleRequest));
    }

    /**
     * 查询列客户群成员表ES
     * @param customerGroupMember
     * @param extInfo
     * @param needFullInfo
     * @param needPage
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroupMember> fetchCustomerGroupMemberListES(CustomerGroupMember customerGroupMember, Map<String, Object> extInfo, Boolean needFullInfo, Boolean needPage, Integer pageNum, Integer pageSize) {
        SingleRequest<CustomerGroupMember> singleRequest = new SingleRequest<>();
        singleRequest.setData(customerGroupMember);
        singleRequest.setNeedFullInfo(needFullInfo);
        singleRequest.setNeedPage(needPage);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        singleRequest.setExtInfo(extInfo);
        return SoaUtil.unpack(customerGroupExportService.fetchCustomerGroupMemberListES(singleRequest));
    }

    /**
     * 批量保存客户群门店关系
     * @param externalList
     */
    public void saveAllCustomerGroupExternal(List<CustomerGroupExternalRequest> externalList) {
       customerGroupExportService.saveAllCustomerGroupExternal(externalList);
    }

    /**
     * 批量插入失败重试记录表
     * @param failRecordList
     */
    public void batchSaveWidCreateRecord(List<CustomerGroupWidCreateRecordDTO> failRecordList) {
        customerGroupExportService.batchSaveWidCreateRecord(failRecordList);
    }

    /**
     * 创建客户群
     * @param customerGroupCreateRequest
     * @return
     */
    public Integer createCustomerGroup(CustomerGroupCreateRequest customerGroupCreateRequest) {
      return SoaUtil.unpack(customerGroupExportService.createCustomerGroup(customerGroupCreateRequest));
    }

    /**
     * 创建客户群成员
     * @param request
     */
    public Boolean batchCreateUpdateGroupMember(CustomerGroupMemberCreateListRequest request) {
      return SoaUtil.unpack(customerGroupExportService.batchCreateUpdateGroupMember(request));
    }

    /**
     * 更新wid创建记录
     * @param dto
     */
    public void updateWidCreateRecord(CustomerGroupWidCreateRecordDTO dto) {
        customerGroupExportService.updateWidCreateRecord(dto);
    }

    /**
     * 批量查询wid创建记录
     * @param recordDto
     * @param needPage
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MultiResponse<CustomerGroupWidCreateRecordDTO> batchFetchWidCreateRecord(CustomerGroupWidCreateRecordDTO recordDto, Boolean needPage, Integer pageNum, Integer pageSize) {
        SingleRequest<CustomerGroupWidCreateRecordDTO> singleRequest = new SingleRequest<>();
        singleRequest.setData(recordDto);
        singleRequest.setNeedPage(needPage);
        singleRequest.setPageNum(pageNum);
        singleRequest.setPageSize(pageSize);
        return SoaUtil.unpack(customerGroupExportService.batchFetchWidCreateRecord(singleRequest));
    }


    /**
     * 删除wid创建记录(物理删除)
     * @param dto
     */
    public void deleteWidCreateRecord(CustomerGroupWidCreateRecordDTO dto) {
        customerGroupExportService.deleteWidCreateRecord(dto);
    }

    /**
     * 批量获取客户群门店关系
     * @param customerGroupExternalRequest
     * @return
     */
    public List<CustomerGroupExternalResponse> batchFetchCustomerGroupExternal(CustomerGroupExternalRequest customerGroupExternalRequest) {
        SingleGroupExternalAttachRequest<CustomerGroupExternalRequest> singleRequest = new SingleGroupExternalAttachRequest<>();
        singleRequest.setData(customerGroupExternalRequest);
        return SoaUtil.unpack(customerGroupExportService.batchFetchCustomerGroupExternal(singleRequest));
    }
}
